package org.dtrd.modules.evaluation.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.DiabetesInfo;
import org.dtrd.modules.evaluation.entity.dto.DiabetesDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdDiabetes;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdDiabetesMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdDiabetesService;
import org.dtrd.modules.util.SchemeUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 糖尿病风险评估 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdDiabetesServiceImpl extends ServiceImpl<DtrdEntEvaluationRdDiabetesMapper, DtrdEntEvaluationRdDiabetes> implements IDtrdEntEvaluationRdDiabetesService {

    @Override
    public DiabetesInfo saveDiabetesInfo(DiabetesDTO dto) {
        int gender = dto.getGender();
        Date birth = dto.getBirth();
        int age = DateUtil.ageOfNow(birth);
        int pressureSystolic = dto.getPressureSystolic();
        float height = dto.getHeight();
        float weight = dto.getWeight();
        float bmi = SchemeUtil.getBmi(height, weight);
        float waistline = dto.getWaistline();
        int diabetesFamily = dto.getDiabetesFamily();
        int score = 0;
        // 年龄（岁）
        if (age < 24) {
            score += 0;
        } else if (age >= 25 && age <= 34) {
            score += 4;
        } else if (age >= 35 && age <= 39) {
            score += 8;
        } else if (age >= 40 && age <= 44) {
            score += 11;
        } else if (age >= 45 && age <= 49) {
            score += 12;
        } else if (age >= 50 && age <= 54) {
            score += 13;
        } else if (age >= 55 && age <= 59) {
            score += 15;
        } else if (age >= 60 && age <= 64) {
            score += 16;
        } else if (age >= 65) {
            score += 18;
        }
        // 体质指数（体重kg/身高m2）
        if (bmi < 22) {
            score += 0;
        } else if (bmi >= 22 && bmi <= 23.9) {
            score += 1;
        } else if (bmi >= 24 && bmi <= 29.9) {
            score += 3;
        } else if (bmi >= 30) {
            score += 5;
        }
        // 腰围（cm）
        if (1 == gender) {
            // 男性
            if (waistline < 75) {
                score += 0;
            } else if (waistline >= 75 && waistline <= 79.9) {
                score += 3;
            } else if (waistline >= 80 && waistline <= 84.9) {
                score += 5;
            } else if (waistline >= 85 && waistline <= 89.9) {
                score += 7;
            } else if (waistline >= 90 && waistline <= 94.9) {
                score += 8;
            } else if (waistline >= 95) {
                score += 10;
            }
        } else {
            // 女性
            if (waistline < 70) {
                score += 0;
            } else if (waistline >= 70 && waistline <= 74.9) {
                score += 3;
            } else if (waistline >= 75 && waistline <= 79.9) {
                score += 5;
            } else if (waistline >= 80 && waistline <= 84.9) {
                score += 7;
            } else if (waistline >= 85 && waistline <= 89.9) {
                score += 8;
            } else if (waistline >= 90) {
                score += 10;
            }
        }
        // 收缩压（mmHg）
        if (pressureSystolic < 110) {
            score += 0;
        } else if (pressureSystolic <= 119) {
            score += 1;
        } else if (pressureSystolic <= 129) {
            score += 3;
        } else if (pressureSystolic <= 139) {
            score += 6;
        } else if (pressureSystolic <= 149) {
            score += 7;
        } else if (pressureSystolic <= 159) {
            score += 8;
        } else if (pressureSystolic >= 160) {
            score += 10;
        }
        // 糖尿病家族史（父母、兄弟姐妹、子女）
        if (1 == diabetesFamily) {
            score += 6;
        } else if (2 == diabetesFamily || 3 == diabetesFamily) {
            score += 0;
        }
        // 性别
        if (1 == gender) {
            // 男性
            score += 2;
        } else if (2 == gender) {
            // 女性
            score += 0;
        }

        String output = "";
        if (score < 25) {
            output = "糖尿病患病风险低<br>目前您的糖尿病患病风险较低。但患病风险并不能作为疾病诊断。并且随年龄增长、其他疾病发展、社会环境变化等，糖尿病风险会有所增加。建议您定期进行血糖检查，有异常及时就诊。预防糖尿病的关键，在于健康饮食、规律运动、合理作息、控制体重、积极治疗原发疾病、戒烟限酒等。";
        } else {
            output = "糖尿病患病风险较高<br>目前您的糖尿病患病风险较高。糖尿病是一种危害严重的疾病，可引起多种并发症，致残致死率较高。建议您监测空腹及三餐后血糖，高于正常及时到医院就诊检查并治疗。或到医院进行OGTT检查，判断是否为糖尿病或糖尿病前期。";
        }
        DtrdEntEvaluationRdDiabetes diabetes = new DtrdEntEvaluationRdDiabetes();
        diabetes.setCreateTime(new Date());
        diabetes.setGender(gender);
        diabetes.setBirth(birth);
        diabetes.setHeight(height);
        diabetes.setWeight(weight);
        diabetes.setWaistline(waistline);
        diabetes.setPressureSystolic(pressureSystolic);
        diabetes.setDiabetesFamily(diabetesFamily);
        diabetes.setScore(score);
        diabetes.setOutput(output);
        if (dto.getPatientId() != null) {
            diabetes.setPatientId(dto.getPatientId());
            baseMapper.insert(diabetes);
        }
        return new DiabetesInfo().parseFromPo(diabetes);
    }

    @Override
    public DiabetesInfo getDiabetesInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdDiabetes> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(DtrdEntEvaluationRdDiabetes::getPatientId, patientId)
                .eq(DtrdEntEvaluationRdDiabetes::getIsDel, 0)
                .orderByDesc(DtrdEntEvaluationRdDiabetes::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdDiabetes diabetes = baseMapper.selectOne(lambdaQueryWrapper);
        DiabetesInfo info = new DiabetesInfo();
        if (diabetes != null) {
            info = info.parseFromPo(diabetes);
        }
        return info;
    }
}
